package com.ruoyi.project.jxc.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jxc.domain.JxcDrug;
import com.ruoyi.project.jxc.domain.JxcMf;
import com.ruoyi.project.jxc.service.IJxcDrugService;
import com.ruoyi.project.jxc.service.IJxcMfService;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/jxc/drug")
public class JxcDrugController extends BaseController {
    @Autowired
    private IJxcDrugService drugService;
    @Autowired
    private IJxcMfService mfService;
    @PreAuthorize("@ss.hasPermi('jxc:mf:list')")
    @GetMapping("/list")
    public TableDataInfo list(JxcDrug drug)
    {
        startPage();
        List<JxcDrug> jxcDrugs = drugService.selectDrugList(drug);
        return getDataTable(jxcDrugs);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxc:drug:query')")
    @GetMapping(value = { "/mfList"})
    public AjaxResult getInfo()
    {
        AjaxResult ajax = AjaxResult.success();
        List<JxcMf> mfs = mfService.selectMfAll();
        ajax.put("mfs", mfs);
        return ajax;
    }
    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('jxc:drug:add')")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody JxcDrug drug)
    {
        if (UserConstants.NOT_UNIQUE.equals(drugService.checkDrugNameUnique(drug)))
        {
            return AjaxResult.error("新增药品'" + drug.getDrugName() + "'失败，药品名称已存在");
        }
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugService.insertDrug(drug));
    }
    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxc:drug:edit')")
    @GetMapping(value = "/{drugId}")
    public AjaxResult getInfo(@PathVariable Long drugId)
    {
        return AjaxResult.success(drugService.selectDrugById(drugId));
    }
    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('jxc:drug:edit')")
    @Log(title = "生产厂家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody JxcDrug drug)
    {
        if (UserConstants.NOT_UNIQUE.equals(drugService.checkDrugNameUnique(drug)))
        {
            return AjaxResult.error("新增药品'" + drug.getDrugName() + "'失败，药品名称已存在");
        }
        drug.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(drugService.updateDrug(drug));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('jxc:drug:remove')")
    @Log(title = "厂家管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugId}")
    public AjaxResult remove(@PathVariable Long[] drugId)
    {

        return toAjax(drugService.deleteDrugById(drugId));
    }
}
